package com.cqrt.controller;


import cn.hutool.core.collection.CollUtil;
import com.cqrt.constant.RTErrorCodeEnum;
import com.cqrt.constant.RouteConstant;
import com.cqrt.dto.RTTemplateLinkDocDTO;
import com.cqrt.entity.RTKnowTempDocDO;
import com.cqrt.service.RTProjectKnowledgeLinkService;
import com.hustcad.plm.rpm.model.dto.remote.BatchInsertDocVO;
import com.ty.ppm.model.constant.ApiPathConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ty.basic.response.ResponseResult;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import com.cqrt.entity.RTKnowTempVO;


@RestController
@Api(tags = "知识推送")
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "knowledge")
public class RTProjectKnowledgeLinkController {

    @Resource
    private RTProjectKnowledgeLinkService rtProjectKnowledgeLinkService;

    @PutMapping("/addDoc")
    @ApiOperation(value = "新增知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<T> insertDoc(@RequestBody BatchInsertDocVO batchInsetDocVO) {
        rtProjectKnowledgeLinkService.addTempLinkDoc(batchInsetDocVO);
        return ResponseResult.success();
    }

    @PutMapping("/addTempDoc")
    @ApiOperation(value = "在模板新增知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<T> insertTempDoc(@RequestBody List<RTKnowTempDocDO> rtKnowTempDocDO) {
        rtProjectKnowledgeLinkService.addTempKnowDoc(rtKnowTempDocDO);
        return ResponseResult.success();
    }
    @PostMapping("/queryTempDoc")
    @ApiOperation(value = "在模板查询知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<List<RTKnowTempVO>> queryTempDoc(@RequestBody BigInteger planActivityId) {
        List<RTKnowTempDocDO> rtKnowTempDocDOS = rtProjectKnowledgeLinkService.queryTempKnowDoc(planActivityId);
        List<RTKnowTempVO> rtKnowTempDocVOS = new ArrayList<>();
        for (RTKnowTempDocDO rtKnowTempDocDO1 : rtKnowTempDocDOS){
            RTKnowTempVO rtKnowTempVO = new RTKnowTempVO();
            rtKnowTempVO.setProjectId(rtKnowTempDocDO1.getProjectId().toString());
            rtKnowTempVO.setOid(rtKnowTempDocDO1.getOid().toString());
            rtKnowTempVO.setOType(rtKnowTempDocDO1.getOType());
            rtKnowTempVO.setDisplayVersion(rtKnowTempDocDO1.getDisplayVersion());
            rtKnowTempVO.setIdentifierDisplayName(rtKnowTempDocDO1.getIdentifierDisplayName());
            rtKnowTempVO.setDocOType(rtKnowTempDocDO1.getDocOType());
            rtKnowTempVO.setObjectNumber(rtKnowTempDocDO1.getObjectNumber());
            rtKnowTempVO.setLatestVersionID(rtKnowTempDocDO1.getLatestVersionID().toString());
            rtKnowTempVO.setPlanActiveOid(rtKnowTempDocDO1.getPlanActiveOid().toString());
            rtKnowTempVO.setPlanOid(rtKnowTempDocDO1.getPlanOid().toString());
            rtKnowTempVO.setJobName(rtKnowTempDocDO1.getJobName());
            rtKnowTempDocVOS.add(rtKnowTempVO);
        }

        return ResponseResult.success(rtKnowTempDocVOS);
    }

    @PostMapping("/deleteTempDoc")
    @ApiOperation(value = "在模板删除知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<T> deleteTempDoc(@RequestBody List<RTKnowTempDocDO> rtKnowTempDocDOS) {
         for (RTKnowTempDocDO rtKnowTempDocDO : rtKnowTempDocDOS){
             List<String> list = rtProjectKnowledgeLinkService.selectIsTemp(rtKnowTempDocDO.getIdentifierDisplayName(),
                                                                            rtKnowTempDocDO.getJobName());
             if (CollUtil.isEmpty(list)) {
                 throw RTErrorCodeEnum.KNOW_IS_TEMPLATE.getException("模板不允许修改");
             }
             rtProjectKnowledgeLinkService.deleteTempDoc(rtKnowTempDocDO.getIdentifierDisplayName(),rtKnowTempDocDO.getJobName());
         }
        return ResponseResult.success();
    }
    //模板计划ID ---  项目计划ID

    @GetMapping("/queryTemplateDetailByOid")
    @ApiOperation(value = "根据计划id查询知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<List<RTTemplateLinkDocDTO>> queryDetailByOid(BigInteger oid) {
        List<RTTemplateLinkDocDTO> rtTemplateLinkDocDTOS = rtProjectKnowledgeLinkService.queryTemplateDetailByOid(oid);
        return ResponseResult.success(rtTemplateLinkDocDTOS);
    }

    @PostMapping("/updateDocDetail")
    @ApiOperation(value = "修改知识文档", produces = "application/json; charset=utf-8")
    public ResponseResult<T> updateDocDetail(@RequestBody List<RTTemplateLinkDocDTO> rtTemplateLinkDocDTOList) {
        rtProjectKnowledgeLinkService.updateDocDetail(rtTemplateLinkDocDTOList);
        return ResponseResult.success();
    }

    @PostMapping("/uploadDoc")
    @ApiOperation(value = "下载知识文档", produces = "application/json; charset=utf-8")
    public void uploadDoc( @RequestBody RTTemplateLinkDocDTO rtTemplateLinkDocDTO) {
        rtProjectKnowledgeLinkService.uploadDoc(rtTemplateLinkDocDTO);
    }






}
